package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.mo.MessageObject;
import cn.zj.logistics.pojo.Role;
import cn.zj.logistics.pojo.RoleExample;
import cn.zj.logistics.pojo.RoleExample.Criteria;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.service.RoleService;
import cn.zj.logistics.service.UserService;

@Controller
@RequestMapping("/role")
public class RoleController {
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	
	
	//页面跳转功能
	@RequestMapping("/rolePage")
	@RequiresPermissions("role:rolePage")
	public String rolePage() {
		
		return "rolePage";
	}
	
	@RequestMapping("/roleEdit")
	public String roleEdit(Model m,Long roleId) {
		
		//如果用户id不等于空，说明是修改操作，查询用户对象共享过去，以供回显
		if(roleId !=null) {
			Role role = roleService.selectByPrimaryKey(roleId);
			m.addAttribute("role", role);
		}
		
		
		return "roleEdit";
	}
	
	
	
	
	//新增功能
	@RequestMapping("/insert")
	@RequiresPermissions("role:insert")
	@ResponseBody
	public MessageObject insert(Role role) {
		
		
		MessageObject mo = MessageObject.createMessageObject(0, "新增数据失败，请联系管理员");
		
		int row = roleService.insert(role);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "新增数据成功");
		}
		
		return mo;
	}
	//修改功能
	@RequiresPermissions("role:update")
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(Role role) {
		
		
		MessageObject mo = MessageObject.createMessageObject(0, "修改数据失败，请联系管理员");
		
		int row = roleService.updateByPrimaryKeySelective(role);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "修改数据成功");
		}
		
		return mo;
	}
	
	
	//数据列表功能
	@ResponseBody
	@RequiresPermissions("role:list")
	@RequestMapping("/list")
	public PageInfo<Role> list(@RequestParam(defaultValue = "1") int pageNum,
				@RequestParam(defaultValue = "10") int pageSize,String keyword){
		
		
		PageHelper.startPage(pageNum, pageSize);
		
		RoleExample example = new RoleExample();
		
		//条件搜索查询
		
		if(StringUtils.isNotBlank(keyword)) {
			//角色名称模糊查询
			Criteria criteria = example.createCriteria();
			criteria.andRolenameLike("%"+keyword+"%");
			
		}
		List<Role> roles = roleService.selectByExample(example);

		PageInfo<Role> pageInfo = new PageInfo<Role>(roles);
		return pageInfo;
	}
	
	@RequiresPermissions("role:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long roleId) {
		/*
		 * 删除前，先要根据角色id去管理员表中查询是否有管理员还拥有此角色
		 * 如果有：不能删除
		 */
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<User> users = userService.selectByExample(example);
		
		
		
		//创建消息对象
		MessageObject mo = MessageObject.createMessageObject(0, "删除数据失败，请联系管理员");
		
		if(users.size() > 0) {
			mo = MessageObject.createMessageObject(0, "此角色还有用户，不能删除");
		}else {
			int row = roleService.deleteByPrimaryKey(roleId);
			
			if(row == 1) {
				mo = MessageObject.createMessageObject(1, "删除数据成功");
			}
			
		}
		
		
		return mo;
	}
	

	
}
